package action;

import global.Constants;
import global.beanUtils.BeanUtils;
import global.security.SessionUtils;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.AccountService;
import util.DateUtils;
import util.ServletHelp;
import util.StringUtils;
import domain.Account;

/**
 * 账目Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class AccountAction extends BaseAction {

	/**
	 * 账目Service
	 * */
	private AccountService accountService = null;

	/**
	 * @param accountService 账目Service
	 */
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}


	/**
	 * 根据条件查询账目信息
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		Account paramAccount = new Account();
		BeanUtils.populate(paramAccount, map);

		// 取得账目列表
		List<Account> accountList = accountService
				.selectByCriteriaForPaging(paramAccount);
		JSONArray datas = new JSONArray();
		if(null != accountList){
			datas = JSONArray.fromObject(accountList);
		}
		int count = accountService.countByCriteria(paramAccount);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		res.put("datas", datas);
		res.put("results", count);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 根据账目ID查询单个账目
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 账目ID
		String accountId = request.getParameter("accountId");
		// 取得账目分类
		Account account = accountService.selectByPrimaryKey(Integer.parseInt(accountId));

		// 返回结果
		JSONObject res = JSONObject.fromObject(account);
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 查询账目统计值
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getSum(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		Account paramAccount = new Account();
		BeanUtils.populate(paramAccount, map);
		// 收入合计
		paramAccount.setAccountType(Constants.ACCOUNT_TYPE_1);
		double inMoney = accountService.getTotalMoney(paramAccount);
		// 支出合计
		paramAccount.setAccountType(Constants.ACCOUNT_TYPE_2);
		double outMoney = accountService.getTotalMoney(paramAccount);
		// 当前收入合计
		paramAccount.setAccountType(Constants.ACCOUNT_TYPE_1);
		double currentInMoney = accountService.getCurrentMoney(paramAccount);
		// 当前支出合计
		paramAccount.setAccountType(Constants.ACCOUNT_TYPE_2);
		double currentOutMoney = accountService.getCurrentMoney(paramAccount);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		res.put("inMoney", inMoney);
		res.put("outMoney", outMoney);
		res.put("balance", new Double(new DecimalFormat(".00").format(inMoney - outMoney)));
		res.put("currentInMoney", currentInMoney);
		res.put("currentOutMoney", currentOutMoney);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存账目信息
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		Account account = new Account();
		BeanUtils.populate(account, map);
		account.setAccountDate(DateUtils.formatStr2Date(account.getAccountDateStr(), DateUtils.DATE_PATTON_1));
		account.setDelflag(Constants.DEL_FLAG_1);
		account.setStatus(Constants.STATUS_1);

		// 方法区分
		String type = request.getParameter("type");
		if (type.equals(Constants.TYPE_CREATE)) {
			// 添加账目
			accountService.insert(account);
		} else if (type.equals(Constants.TYPE_UPDATE)) {
			// 账目ID
			String accountId = request.getParameter("accountId");
			account.setAccountId(Integer.parseInt(accountId));

			// 更新账目
			accountService.update(account);

		}
		// 构造查询结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		String result = res.toString();
		// 返回查询结果
		ServletHelp.outRequestForJson(request, response, result);
		return null;
	}

	/**
	 * 根据账目ID删除账目
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 账目ID
		String accountIdStr = request.getParameter("accountIds");
		// 封装账目列表
		if (StringUtils.isNotEmpty(accountIdStr)) {
			// 删除账目
			accountService.delete(accountIdStr);
		}
		
		// 构造查询结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		String result = res.toString();
		// 返回查询结果
		ServletHelp.outRequestForJson(request, response, result);
		return null;
	}
	/**
	 * 根据条件分组查询账目收支金额
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getGroupSumMoney(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		Account paramAccount = new Account();
		BeanUtils.populate(paramAccount, map);
		// 当前用户
		paramAccount.setCreateUser(SessionUtils.getCurrentUserId());

		// 取得账目列表
		List<Account> accountList = accountService.getGroupSumMoney(paramAccount);
		JSONArray datas = new JSONArray();
		if(null != accountList){
			datas = JSONArray.fromObject(accountList);
		}
		int count = accountService.countByCriteria(paramAccount);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		res.put("datas", datas);
		res.put("results", count);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
